import getRequest from '@/server/getConfig';
import instance from '@/server/axiosInstances';

const service = async(data) => {
  const BASE_URL = await getRequest();
  return new Promise((resolve, reject) => {
    instance({
      baseURL: data.baseURL || BASE_URL.data.ApiUrl,
      ...data
    }).then((res) => {
      try {
        const {
          success
        } = res;
        if (success) {
          resolve({
            ...res,
            success: true,
            message: res.message,
            data: res && res.data
          });
        } else {
          if (data.responseType === 'blob') {
            resolve({
              ...res,
              success: true,
              message: res.message,
              data: res
            });
          } else {
            resolve({
              ...res,
              status: false,
              message: res.data.message || (res.message),
              data: res && res.data
            });
          }
        }
      } catch (e) {
        reject({
          success: false,
          data: null,
          message: e
        });
      }
    });
  });
};

const request = data => {
  return service(data);
};

export default request;
